<template>
  <nextAuditEmp :item-id="itemId" :title="auditParam.DocTitle + '审批单'" :visible.sync="visible" @save="save" @close="close" />
</template>

<script>
import * as flowAuditApi from '@/flow/api/flowAudit'
import nextAuditEmp from './nextAuditEmp.vue'
export default {
  components: {
    nextAuditEmp
  },
  props: {
    auditParam: {
      type: Object,
      default: () => {
        return {
          AppId: null,
          LinkPk: null,
          LinkType: null,
          DocTitle: null
        }
      }
    },
    isSubmit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      itemId: null,
      logId: null,
      visible: false
    }
  },
  computed: {
    comId() {
      return this.$store.getters.curComId
    }
  },
  watch: {
    isSubmit: {
      handler(val) {
        if (val) {
          this.submit()
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    async save(next) {
      next.AuditResult = 0
      next.ItemId = this.itemId
      await flowAuditApi.Submit(this.logId, next)
      this.$message({
        type: 'success',
        message: '提交成功!'
      })
      this.$emit('update:isSubmit', false)
      this.$emit('success')
    },
    close() {
      this.$emit('update:isSubmit', false)
    },
    async submit() {
      const res = await flowAuditApi.Begin(this.auditParam)
      if (res.IsNext) {
        this.itemId = res.ItemId
        this.logId = res.LogId
        this.visible = true
      } else {
        this.$emit('update:isSubmit', false)
        this.$emit('success')
        this.$router.replace({ name: 'appAudit', params: { id: res.ItemId }})
      }
    }
  }
}
</script>
